package com.atguigu.gulimall.product.app;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import com.atguigu.gulimall.product.vo.BrandVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 品牌分类关联
 *
 * @author DuGuanghan
 * @since 2024-02-02 16:12:31
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService relationService;
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = relationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = relationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 获取品牌关联的分类
     *
     * @param brandId 品牌 ID
     * @return {@link R }
     * @author DuGuanghan
     * @since 2024-01-19 09:31:44
     */
    @GetMapping("/catelog/list")
    public R list(@RequestParam(value = "brandId") Long brandId) {
        List<CategoryBrandRelationEntity> list = relationService.list(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));

        return R.ok().put("data", list);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		brandService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		relationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		relationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 获取分类关联的品牌
     *
     * @param catId 分类ID
     * @return {@link R }
     * @author DuGuanghan
     * @since 2024-02-26 14:34:38
     */
    @GetMapping("/brands/list")
    public R getBrandsByCatId(@RequestParam(value = "catId") Long catId) {
        List<BrandEntity> brandEntities = brandService.getBrandsByCatId(catId);

        List<BrandVO> brandVos = brandEntities.stream().map(entity -> {
            BrandVO relationVO = new BrandVO();

            relationVO.setBrandId(entity.getBrandId());
            relationVO.setBrandName(entity.getName());

            return relationVO;
        }).collect(Collectors.toList());

        return R.ok().put("data", brandVos);
    }
}
